package leetcode.Leetcode49;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


class DraftSolution {
    public List<List<String>> groupAnagrams(String[] strs) {
        /*对于每个str，创建其相应的字母统计表统计其次数作为hash表的dict
        */
        Map<String,List<String>> map=new HashMap<String,List<String>>();
        for (String str : strs) {
            int[] flagArr=new int[26];
            for (int i = 0; i < str.length(); i++) {
                //对于每个字母减去a在相应位置+1表示其字母出现次数
                flagArr[str.charAt(i)-'a']+=1;
            }
            //生成str的key,字符+次数
            StringBuffer strKey = new StringBuffer();
            for (int i = 0; i < 26; i++) {
                String strTmp= String.valueOf('a'+i+flagArr[i]);
                strKey.append(strTmp);
            }
            String key=strKey.toString();
            List<String> list=map.getOrDefault(key,new ArrayList<String>());
            list.add(str);
            map.put(key,list);
        }
        return new ArrayList<>(map.values());
    }

    public static void main(String[] args) {
        DraftSolution dra = new DraftSolution();
        List<List<String>> list=dra.groupAnagrams(new String[]{"eat","tea","tan","ate","nat","bat"});
    }
}
